import apiCommon from './api-common';
export default {

  // 获取数据源编码列表
  getDataSourceList(queryType = '', datasourceType = 'noRedis', currPage = '1', pageSize = '50') {
    const list = [{ key: 'queryType', value: queryType }, { key: 'currPage', value: currPage }, { key: 'pageSize', value: pageSize }, { key: 'datasourceType', value: datasourceType }];
    const queryStr = list.reduce((p, c, i)=>{
      if (p) {
        return c.value ? `${p}&${c.key}=${c.value}` : p;
      } else {
        return c.value ? `${c.key}=${c.value}` : '';
      }
    }, '');
    return window._UIDesingerAxios({
      method: 'get',
      url: `/dcp/framework/framework/v1/datasource/getList?${queryStr}`,
    });
  },

  /**
   * 根据code获取单个数据源:
   * @param {string} code 默认ado_common_MarketingCloud
   * @param {string} queryType 默认common: 查询公共数据源
   */
  getDataSourceByCode(code = 'ado_common_MarketingCloud') {
    return window._UIDesingerAxios({
      method: 'get',
      url: `/dcp/framework/framework/v1/datasource/getByCode?code=${code}&queryType=common`,
    });
  },

  /**
   * 获取数据表列表
   * @param {string} datasourceCode 数据源编码code
   */
  getDataTableList(datasourceCode) {
    return window._UIDesingerAxios({
      method: 'get',
      url: `/dcp/framework/framework/v1/table/getList?datasourceCode=${datasourceCode}`,
    });
  },

  /**
   * 获取API配置的字段
   * @param {string} id API对应的id
   */
  getMappingOptions(id) {
    return window._UIDesingerAxios({
      method: 'get',
      url: `/dcp/framework/framework/v1/api/getById?id=${id}`,
    });
  },

  /**
   * 获取 API编码
   * @param {string} datasourceCode 数据源编码code
   * @param {boolean} isCommonDatasource xMiner公用数据库的数据源需要传该参数, 值为true
   */
  getAPICodeList(datasourceCode, isCommonDatasource) {
    return window._UIDesingerAxios({
      method: 'get',
      url: `/dcp/framework/framework/v1/api/getList?datasourceCode=${datasourceCode}` +
        `${isCommonDatasource ? `&isCommonDatasource=${isCommonDatasource}` : ''}`,
    });
  },

  /**
   * 获取用于渲染图表的数据
   * @param {object} params 接口入参
   */
  getChartData(params, data) {
    const _params = apiCommon.clearParams(params);
    const { actType, switchEnable } = data?.autoRefresh || {};
    return window._UIDesingerAxios({
      method: 'post',
      url: '/quality/api/v1/dataConvert/getByApiCode',
      data: _params,
      headers: {
        'Content-Type': 'application/json',
      },
      isPolling: switchEnable && actType === 'pull',
    });
  },

  // xMiner异步导出接口
  asyncExportTableData(params) {
    const _params = apiCommon.clearParams(params);
    return window._UIDesingerAxios({
      method: 'post',
      url: '/dcp/data/data/v1/report/asyncExport',
      data: _params,
      timeout: 120000,
      headers: {
        'Content-Type': 'application/json',
      },
    });
  },
};
